package com.study.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.PurAppDao;
import com.study.model.jdao.PurAppDetailsDao;
import com.study.model.mdao.PurAppMapper;
import com.study.model.pojo.PurApp;
import com.study.model.pojo.PurAppDetail;

@Service
public class PurAppServices {

	@Autowired
	PurAppDao pad;

	@Autowired
	PurAppDetailsDao padd;

	@Autowired
	PurAppMapper pam;

	/***
	 * 添加采购申请
	 * 
	 * @param pa
	 * @return
	 */
	public void addPurApp(PurApp app) {

		pad.save(app);

	}

	/**
	 * 删除主表
	 * 
	 * @param appId
	 */
	public void delPurApp(String appId) {
		pam.deletePurApp(appId);
	}

	/**
	 * 删除从表
	 * 
	 * @param appId
	 */
	public void delAppDetail(String appId) {
		pam.deleteAppDetail(appId);
	}

	/**
	 * 添加申请明细
	 * 
	 * @param purAppDetails
	 */
	public void saveAppDetails(List<PurAppDetail> purAppDetails) {
		/*
		 * String type = "SQXQ"; AutoIdFactory factory = new AutoIdFactory();
		 * PurAppDetail detail = new PurAppDetail();
		 * 
		 * for (int i = 0; i < purAppDetails.size(); i++) { String billcode =
		 * pam.findPurAppDetailMaxId(); String id = factory.getId(type, billcode);
		 * 
		 * System.out.println("111--" + purAppDetails.get(i).getProId());
		 * purAppDetails.get(i).setAppdetailId(id);
		 * System.out.println(purAppDetails.get(i).getAppdetailId());
		 * System.out.println("---------");
		 * 
		 * pam.saveAppDetail(detail); }
		 */
		pam.saveAppDetails(purAppDetails);
	}

	// 添加明细
	public void addPurAppDetail(Map map) {
		pam.saveAppDetail(map);
	}

	public void updateToOne(String appId) {
		pam.updateStateToOne(appId);
	}

	/**
	 * 查询全部采购申请信息
	 * 
	 * @return
	 */
	public List<PurApp> findPurAppList(String theme, String applicant, String appdept, String state) {
		System.out.println("serv--" + appdept);
		int st = this.ConverState(state);
		System.out.println("state:" + st);
		return pam.findPurApps(theme, applicant, appdept, st);
	}

	/**
	 * 根据ID查询单条申请全部信息
	 * 
	 * @param appId
	 * @return
	 */
	public List<Map<String, Object>> findPurAppById(String appId) {
		System.out.println("hhh" + appId);
		Map<String, Object> appMap = pam.findPurApp(appId);
		List<Map<String, Object>> details = pam.findAppDetailsAll(appId);
		List list = new ArrayList<Map<String, Object>>();
		list.add(appMap);
		list.add(details);
		return list;
	}

	/**
	 * 转换状态
	 * 
	 * @param state
	 * @return
	 */
	public int ConverState(String state) {
		int stat = 0;
		if (state != null && state != "") {

			if (state.equals("审批中")) {
				stat = 1;
			} else if (state.equals("已通过")) {
				stat = 2;
			} else if (state.equals("未通过")) {
				stat = 3;
			}

		}
		return stat;
	}
}
